package com.gitee.aurora.job;

import com.gitee.aurora.core.utils.ResultVO;
import com.gitee.aurora.server.service.PtConfigService;
import com.gitee.aurora.server.vo.DataCheckVo;
import com.gitee.aurora.server.vo.PtcjConfig;
import com.gitee.aurora.comm.Constant;
import com.gitee.aurora.utils.DateUtils;
import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author liaofuxing
 * @date 2022/09/20 23:35
 **/
public class DataCheckBaMengJob extends QuartzJobBean {

    private static final Logger LOGGER = LoggerFactory.getLogger(DataCheckBaMengJob.class);

    /**
     * 数据中心code
     */
    @Value("${data.bamengcode}")
    private String bamengcode;
    /**
     * 数据中心
     */
    @Value("${data.bamengip}")
    private String bamengip;
    /**
     * 请求数据中心插入接口
     */
    @Value("${http.dszxjt}")
    private String  dszxjt;
    @Autowired
    private PtConfigService ptConfigService;
    @Resource
    private RestTemplate restTemplate;


    @Override
    protected void executeInternal(JobExecutionContext context) {
        System.out.println("-------------------------------自动任务执行成功--------------------------------------");
        try{
            List<String> baqList=null;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String jdip="";
            DataCheckVo checkVo=new DataCheckVo();
            PtcjConfig ptcjConfig=new PtcjConfig();
            HttpHeaders headers = new HttpHeaders();
            HttpEntity<DataCheckVo> httpEntity = new HttpEntity<>(checkVo,headers);
            ptcjConfig.setCode(bamengcode);
            ptcjConfig.setPtCode(Constant.baqlx);
            //查询办案区ip
            if(ptConfigService.queryPtxjinfoList(ptcjConfig).size()>0&& ptConfigService.queryPtxjinfoList(ptcjConfig)!=null) {
                baqList = ptConfigService.queryPtxjinfoList(ptcjConfig).stream().map(PtcjConfig::getPtIp).collect(Collectors.toList());

            }
            ptcjConfig.setPtCode(Constant.jdlx);
            //查询监督ip
            if(ptConfigService.queryPtxjinfoList(ptcjConfig).size()>0&& ptConfigService.queryPtxjinfoList(ptcjConfig)!=null) {
                jdip = ptConfigService.queryPtxjinfoList(ptcjConfig).get(0).getPtIp();
            }
            checkVo.setSjzxIp(Collections.singletonList(bamengip));
            checkVo.setZfjdIp(jdip);
            checkVo.setBaqIp(baqList);
            checkVo.setEndTime(simpleDateFormat.format(DateUtils.getEndTime(new Date())));
            checkVo.setStartTime(simpleDateFormat.format(DateUtils.getStartTime(new Date())));

            restTemplate.exchange(dszxjt.replace(Constant.ip,bamengip), HttpMethod.POST, httpEntity, ResultVO.class);
        }catch (Exception e){
            e.printStackTrace();
        }

        System.out.println("-------------------------------自动任务执行成功--------------------------------------");
    }
}
